﻿using System.Runtime.CompilerServices;
using Unity.VisualScripting;
using UnityEditor;
using UnityEngine;

namespace Common
{
    public class Singleton<T> : MonoBehaviour where T : Singleton<T>
    {
        private static T _instance;

        public static T GetInstance()
        {
            MakeSureInstance();
            return _instance;
        }

        protected virtual void Awake()
        {
            if (_instance != null && _instance != this)
            {
                Destroy(gameObject);
                return;
            }
            _instance = this as T;
        }

        private static void MakeSureInstance()
        {
            if (_instance == null)
            {
                var obj = new GameObject();
                _instance = obj.AddComponent<T>();
                DontDestroyOnLoad(obj);
            }
        }
    }
}